package com.example.demo.entity.user;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.bson.types.ObjectId;
import org.omg.CORBA.Object;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description:
 * @author: cj
 * @create: 16-02-07 16:50:16
 */
@Data
@AllArgsConstructor
public class LoginUser implements Serializable, UserDetails {
    /**
     * 密码
     */
    private String password;
    private String id;
    /**
     * 手机号
     */
    private String phone;
    /**
     * 账号
     */
    private String account;
    /**
     * 头像
     */
    private String headImg;
    /**
     * 个人介绍
     */
    private String hi;
    /**
     * 用户类型 9 超级 0 null 普通
     */
    private String type;
    /**
     * 姓名
     */
    private String name;

    private List<String> permission;

    private List<String> roles;

    private List<GrantedAuthority> authorities;

    private String accessToken;

    public LoginUser() {

    }


    /**
     * @return 权限
     */
    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {

//        if (authorities != null) {
//            return authorities;
//        }
//        return permission.stream().map( SimpleGrantedAuthority::new )
//                .collect( Collectors.toList() );
        if (authorities != null) {
            return authorities;
        }
        List<GrantedAuthority> list = new ArrayList<>();
        if (permission == null) {
            return null;
        }
        for (String s : permission) {
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority( s );
            list.add( simpleGrantedAuthority );
        }
        return list;
    }

    /**
     * @return 密码
     */
    @Override
    public String getPassword() {
        return password;
    }

    /**
     * @return 用户名
     */
    @Override
    public String getUsername() {
        return account;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * @return 是否可用
     */
    @Override
    public boolean isEnabled() {
        return true;
    }
}
